<template>
  <div>
    <group>
      <cell is-link link="/user/info">
          <span slot="title">
            <span>个人信息</span>
            <badge :text=authStatusText style="background-color: #0BB20C"></badge>
          </span>
      </cell>
      <cell title="我的办件" is-link link="/user/user-matter"></cell>
      <!--<cell title="我的预约" is-link link="/user/appoint"></cell>-->
      <cell title="我的咨询" is-link link="/user/consult"></cell>
      <!--<cell title="我的举报" is-link link="/user/report"></cell>-->
      <!--<cell title="我的报警" is-link link="/user/alarm"></cell>-->
      <!--<cell title="信息绑定" is-link link="/user/bindInfo"></cell>-->
    </group>
    <div class="btn-box" style="margin-top: 30px;">
      <x-button type="primary" :link="oauthAddress">实名核身</x-button>
    </div>
    <div class="btn-box" style="margin-top: 30px;">
      <x-button type="default" plain @click.native="goOut">退出登录</x-button>

    </div>
    <alert v-model="showAlert" :title="title" :content="content" @on-hide="onHide"></alert>
    <div>
      <alert v-model="alertShow" title="提示"> {{ alertMessage }}</alert>
    </div>
  </div>
</template>
<script>
  import {Cell, XButton, Badge, Group, Alert} from 'vux'

  export default {
    data() {
      return {

        sessionUser: JSON.parse(localStorage.getItem('user')),
        showAlert: false,
        title: '',
        content: '',
        authStatusText: '',
        oauthAddress: '',
        alertShow: false,
        alertMessage: ''
      }
    },
    created() {
      this.initData();
      this.oauthAddress = global.memberApiUrl + '/member/tencent/confirm?userGuid=' + this.sessionUser.guid;
//      this.oauthAddress = 'http://www.baidu.com';

//      console.log(this.$router.currentRoute.meta.requireAuth);
      //console.log(this.$store.state.loginState);
      //console.log(this.$router.currentRoute.query);

      //console.log(this.sessionUser.memberStatus);
      if (this.$router.currentRoute.query.returnUrl) {
        if (this.sessionUser.memberStatus == null) {
          this.alertShow = true;
          this.alertMessage = '您未完成实名核身，请完成实名核身后再使用该功能';
        } else {
          if (this.sessionUser.memberStatus.identityStatus != '1') {
            this.alertShow = true;
            this.alertMessage = '您未完成实名核身，请完成实名核身后再使用该功能';
          }
        }
      }

    },
    methods: {
      initData: function () {
        let _self = this;
        this.axios({
          method: 'get',
          url: global.memberApiUrl + '/member/' + _self.sessionUser.guid
        })
          .then(function (response) {
            if (response.status == "200" && response.data.succ) {
              var member = response.data.data;
              _self.$store.dispatch("userSignin", {
                guid: member.guid,
                accountId: member.accountId,
                realName: member.realName,
                openId: member.wxOpenId,
                memberStatus: member.memberStatus  //是否实名认证 0未认证1已认证-1未通过
              });
              //console.info(member);
              _self.showAuthText(member);
            }
          })
          .catch(function (response) {
            // console.log(response);
          });
      },
      showAuthText(member) {
        let _self = this;
        var memberStatus = member.memberStatus;
        if (memberStatus) {
          var identityStatus = memberStatus.identityStatus;
          if (identityStatus == '1') {
            _self.authStatusText = "已认证";
          } else if (identityStatus == '-1') {
            _self.authStatusText = "未通过认证";
          } else if (identityStatus == '0') {
            _self.authStatusText = "未认证";
          }
        } else {
          _self.authStatusText = "未认证";
        }
      },
      goOut() {
        let self = this;
        self.showAlert = true;
        self.title = '退出';
        self.content = '确定要退出吗？';
      },
      onHide() {
        this.$store.dispatch("userSignout");//删除本地存储的用户信息
        this.showAlert = false;
        this.$router.push('/');
      }
    },
    components: {
      Group,
      XButton,
      Badge,
      Cell,
      Alert
    }
  }
</script>
